#########################################
######### Balancing Tables  ############
########   October 10, 2018 ######
######## Rerun: December 16, 2022

rm(list=ls())
library(stargazer)
library(ggplot2)
library(foreign)
######GEO DATA####
# setwd("~/Dropbox/Personal Research 2017")


#########################################
##### Create  Bandwidths ###############
geo2001=read.csv("~/Dropbox/Personal Research 2017/replications/geocensus2001_nov16.csv")

#Distance to Mysore-Bombay Border
rd10.mb=geo2001[which(geo2001$NEAR_DIST_border1<10000),] #20 km
table(rd10.mb$border1)

#Distance to Hyderabad-Bombay Border
rd10.hb=geo2001[which(geo2001$NEAR_DIST_border2<10000),] #20 km
table(rd10.hb$border2)


#Balancing and Different Borders 
#Mysore- Bombay

#10 km Mysore-Bombay
t1=t.test(rd10.mb$TOT_POP[rd10.mb$border1==1], rd10.mb$TOT_POP[rd10.mb$border1==0])
t2=t.test(rd10.mb$TOT_SC[rd10.mb$border1==1], rd10.mb$TOT_SC[rd10.mb$border1==0])
t3=t.test(rd10.mb$TOT_ST[rd10.mb$border1==1], rd10.mb$TOT_ST[rd10.mb$border1==0])
t5=t.test(rd10.mb$Slope[rd10.mb$border1==1], rd10.mb$Slope[rd10.mb$border1==0])
t6=t.test(rd10.mb$TerrainRug[rd10.mb$border1==1], rd10.mb$TerrainRug[rd10.mb$border1==0])
est=rbind(t1$estimate, t2$estimate, t3$estimate, t5$estimate, t6$estimate)
pval=rbind(t1$p.value, t2$p.value, t3$p.value,  t5$p.value, t6$p.value)
mb.10=cbind(est, pval)
rownames(mb.10)=c("Total Population", 
                  "Total Scheduled Castes Pop",
                  "Total Scheduled Tribes Pop", 
                  "Slope", "Terrain Rugness")
colnames(mb.10)=c("Mean Tr", "Mean Cont", "T-Test P.Value")
stargazer(mb.10, digits = 3)

##############################################
######Hyderabad-Bombay Border

#10 km Hyderabad-Bombay
t1=t.test(rd10.hb$TOT_POP[rd10.hb$border2==1], rd10.hb$TOT_POP[rd10.hb$border2==0])
t2=t.test(rd10.hb$TOT_SC[rd10.hb$border2==1], rd10.hb$TOT_SC[rd10.hb$border2==0])
t3=t.test(rd10.hb$TOT_ST[rd10.hb$border2==1], rd10.hb$TOT_ST[rd10.hb$border2==0])
t5=t.test(rd10.hb$Slope[rd10.hb$border2==1], rd10.hb$Slope[rd10.hb$border2==0])
t6=t.test(rd10.hb$TerrainRug[rd10.hb$border2==1], rd10.hb$TerrainRug[rd10.hb$border2==0])
est=rbind(t1$estimate, t2$estimate, t3$estimate, t5$estimate, t6$estimate)
pval=rbind(t1$p.value, t2$p.value, t3$p.value, t5$p.value, t6$p.value)
hb.10=cbind(est, pval)
rownames(hb.10)=c("Total Population", 
                  "Total Scheduled Castes Pop",
                  "Total Scheduled Tribes Pop", 
                  "Slope", "Terrain Rugness")
colnames(hb.10)=c("Mean Tr", "Mean Cont", "T-Test P.Value")
stargazer(hb.10, digits = 3)
